package com.ace3l.legal.backend.dao;

import com.ace3l.legal.backend.domain.Project;
import com.ace3l.legal.backend.domain.LawFirm;
import com.ace3l.legal.backend.exceptions.ProjectException;
import com.ace3l.legal.backend.exceptions.Ace3LError;

import javax.persistence.Query;
import javax.persistence.NoResultException;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Date;
import java.io.Serializable;
import java.text.SimpleDateFormat;

/**
 * Created by IntelliJ IDEA.
 * User: ageer
 * Date: Jul 26, 2008
 * Time: 5:27:43 AM
 * To change this template use File | Settings | File Templates.
 */
public class ProjectDao extends ADao{
    public static final String RESULT_FORMAT_LIST_OF_DOMAIN = "listdomain";
    public static final String RESULT_FORMAT_LIST_OF_ARRAY = "listarray";
    public static final String RESULT_FORMAT_ARRAY_OF_ARRAY = "arrayarray";

    public Project saveProject(Project myLf) {
        entityManager.persist(myLf);
        return myLf;
    }

    public String[] getProjectsByStatusAbridgedHeader() {
        String[] res = new String[] {"integer_:_id","string_:_title","integer_:_hoursAllotted","date_:_dueDate_:_Y-m-d 00:00:00.0","string_:_lawFirmName"};
        return res;
    }
    public List<Project> getProejctsByStatus(String status) {
        return (List<Project>)getProjectsByStatus(status, false, null, true);
    }
    public List<Project> getProjectsByStatus(String status, boolean isAbridged) {
        return (List<Project>)getProjectsByStatus(status, isAbridged, RESULT_FORMAT_LIST_OF_DOMAIN, true);
    }
    public Object getProjectsByStatus(String status, boolean isAbridged, String format, boolean serialize) {
        if (isAbridged) {
            if (format == null) format = RESULT_FORMAT_LIST_OF_DOMAIN;
            Query q = entityManager.createNamedQuery("projects.bystatus.abridged");
            q.setParameter("status", status);
            List<Object[]> res = q.getResultList();
            if (serialize) {
                Iterator<Object[]> resIt = res.iterator();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                while (resIt.hasNext()) {
                    Object[] row = resIt.next();
                    for (int i = 0; i < row.length; i++) {
                        if (row[i] instanceof java.sql.Date) {
                            row[i] = df.format(row[i]);
                        }
                        else {
                            row[i] = row[i].toString();
                        }
                    }
                }

            }
            if (format.equals(RESULT_FORMAT_LIST_OF_ARRAY)) return res;
            else if (format.equals(RESULT_FORMAT_ARRAY_OF_ARRAY)) {
                return res.toArray(new Object[res.size()][]);
            } else {
                List<Project> projList = new ArrayList<Project>(res.size());
                Iterator<Object[]> resIt = res.iterator();
                while (resIt.hasNext()) {
                    Object[] row = resIt.next();
                    Project nxtProject = new Project();
                    nxtProject.setId((Integer)row[0]);
                    nxtProject.setTitle((String)row[1]);
                    nxtProject.setHoursAllotted((Integer)row[2]);
                    nxtProject.setDueDate((Date)row[3]);
                    LawFirm nxtLawFirm = new LawFirm();
                    nxtLawFirm.setLawFirmName((String)row[4]);
                    nxtProject.setLawFirm(nxtLawFirm);
                    projList.add(nxtProject);
                }
                return projList;
            }
        } else {
            Query q = entityManager.createNamedQuery("projects.bystatus");
            q.setParameter("status", status);
            return q.getResultList();
        }
    }
    public List<Project> getProjectsForLawFirm(Integer id) {
        Query q = entityManager.createNamedQuery("projects.byid");
        q.setParameter(1, id);
        List<Project> myProjects = new ArrayList<Project>();
        try {
            myProjects = q.getResultList();
        } catch (Exception e) { }
        return myProjects;
    }

    public Project getProject(Integer id) throws ProjectException
    {
        Query q = entityManager.createNamedQuery("project.byid");
        q.setParameter("projectId", id);
        Project myProject;
        try {
            myProject = (Project)q.getSingleResult();
        } catch (NoResultException e) {
            throw new ProjectException(Ace3LError.PROJECT_NOT_FOUND, id.toString());
        } catch (Exception e) {
            throw new ProjectException(Ace3LError.DATABASE_ERROR, id.toString());
        }

        return myProject;
    }

}